package cn.yunhe.controller;

import cn.yunhe.model.Role;
import cn.yunhe.view.ExcelExportService;
import cn.yunhe.view.ExcelView;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zs on 2017/12/19.
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/export")
    public ModelAndView export() {
        ModelAndView mv = new ModelAndView();
        //定义Excel视图
        ExcelView ev = new ExcelView(exportService());
        //设置文件名
        ev.setFileName("所有角色.xls");
        //数据，构建对象，省略数据库步骤
        Role role1 = new Role();
        role1.setId(1);
        role1.setRoleName("武松");
        role1.setNote("行者");


        Role role2 = new Role();
        role2.setId(2);
        role2.setRoleName("鲁智深");
        role2.setNote("花和尚");

        Role role3 = new Role();
        role3.setId(3);
        role3.setRoleName("杨志");
        role3.setNote("青面兽");

        List<Role> roleList = new ArrayList<Role>();
        roleList.add(role1);
        roleList.add(role2);
        roleList.add(role3);

        mv.addObject("roleList", roleList);
        mv.setView(ev);

        return mv;
    }


    @SuppressWarnings({"unchecked"})
    private ExcelExportService exportService() {
        return (Map<String, Object> model, Workbook workBook) -> {
            List<Role> roleList = (List<Role>) model.get("roleList");
            Sheet sheet = workBook.createSheet("所有角色");
            Row title = sheet.createRow(0);
            title.createCell(0).setCellValue("编号");
            title.createCell(1).setCellValue("名称");
            title.createCell(2).setCellValue("备注");
            //遍历数据

            for (int i = 0; i < roleList.size(); i++) {
                Role role = roleList.get(i);
                int rowIndex = i + 1;
                Row row = sheet.createRow(rowIndex);
                row.createCell(0).setCellValue(role.getId());
                row.createCell(1).setCellValue(role.getRoleName());
                row.createCell(2).setCellValue(role.getNote());
            }

        };
    }

    @RequestMapping("/up")
    public ModelAndView upfiles(MultipartFile file) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());

        //获取原始的文件名
        String fileName = file.getOriginalFilename();
        file.getContentType();

        //目标地址
        File dest = new File(fileName);

        try {
            file.transferTo(dest);
            mv.addObject("succ", true);
            mv.addObject("msg", "上传成功");

        } catch (IOException e) {
            mv.addObject("succ", false);
            mv.addObject("msg", "上传失败");
            e.printStackTrace();
        }
        return mv;

    }


}
